package com.hg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.mapper.ItemParamMapper;
import com.hg.pojo.ItemParam;
import com.hg.service.IItemParamService;
import com.hg.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 商品规则参数 服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-30
 */
@Service
public class ItemParamServiceImpl extends ServiceImpl<ItemParamMapper, ItemParam> implements IItemParamService {
    @Autowired
    private ItemParamMapper itemParamMapper;

    @Override
    public ItemParam selectItemParamByItemCatId(Long itemCatId) {
        QueryWrapper<ItemParam> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("item_cat_id", itemCatId);
        ItemParam itemParam = itemParamMapper.selectOne(QueryWrapper);
        return itemParam;

    }

    @Override
    public PageResult selectItemParamAll() {
        Page<ItemParam> page = new Page<>(1,10);
        QueryWrapper<ItemParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("updated");
        Page<ItemParam> itemParamPage = itemParamMapper.selectPage(page, queryWrapper);
        return new PageResult(itemParamPage.getSize(),itemParamPage.getPages(),itemParamPage.getRecords());
    }

    @Override
    public void insertItemParam(Long itemCatId, String paramData) {
        ItemParam itemParam = new ItemParam();
        itemParam.setItemCatId(itemCatId);
        itemParam.setParamData(paramData);
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        itemParamMapper.insert(itemParam);
    }

    @Override
    public void deleteItemParamById(Integer id) {
        itemParamMapper.deleteById(id);
    }
}
